//生成菜单
var menuItem = Vue.extend({
    name: 'menu-item',
    props: {item: {}},
    template: [
        '<li>',
        '	<a v-if="item.moduletype === 0" href="javascript:;">',
        '		<i v-if="item.iconaddress != null" :class="item.iconaddress"></i>',
        '		<span>{{item.modulename}}</span>',
        '		<i class="fa fa-angle-left pull-right"></i>',
        '	</a>',
        '	<ul v-if="item.moduletype === 0" class="treeview-menu">',
        '		<menu-item :item="item" v-for="item in item.list"></menu-item>',
        '	</ul>',

        '	<a v-if="item.moduletype === 1 && item.parentguid === 0" :href="\'#\'+item.moduleurl">',
        '		<i v-if="item.iconaddress != null" :class="item.iconaddress"></i>',
        '		<span>{{item.modulename}}</span>',
        '	</a>',
        '	<a v-if="item.moduletype === 1 && item.parentguid != 0" :href="\'#\'+item.moduleurl">',
        '       <i v-if="item.iconaddress != null" :class="item.iconaddress"></i>',
        '       <i v-else class="fa fa-circle-o"></i> {{item.modulename}}</a>',
        '</li>'
    ].join('')
});

//生成消息列表
var messageItem = Vue.extend({
    name: 'message-item',
    props: {item: {}},
    template: [
        '<li>',
        '	<a href="#">',
        '		<i class="ion ion-ios-people info"></i>',
        '		{{item.content}}',
        '	</a>',
        '</li>'
    ].join('')
});

//生成代办列表
var messagecenterItem = Vue.extend({
    name: 'messagecenter-item',
    props: {item: {}},
    template: [
        '<li>',
        '	<a >',
        '		<i class="ion ion-ios-people info"></i>',
        '		{{item.content}}',
        '	</a>',
        '</li>'
    ].join('')
});

//iframe自适应
$(window).on('resize', function () {
	console.log(4);
    var $content = $('.content');
    $content.height($(this).height() - 120);
    $content.find('iframe').each(function () {
        $(this).height($content.height());
    });
}).resize();


//注册菜单组件
Vue.component('menuItem', menuItem);
Vue.component('messageItem', messageItem);
Vue.component('messagecenterItem', messagecenterItem);

var vm = new Vue({
    el: '#rrapp',
    data: {
        user: {},
        menuList: {},
        main: "main.html",
        password: '',
        newPassword: '',
        navTitle: "控制台",
        messageList:{},
        messagecount:0
    },
    methods: {
        getMenuList: function (event) {
            $.getJSON("sys/menu/nav?_" + $.now(), function (r) {
                vm.menuList = r.menuList;
            });
        },
        getMessageList: function () {
       	 	$.getJSON("sys/message/mainlist?_" + $.now(), function (r) {
                vm.messageList = r.messageList;
                vm.messagecount=r.messagecount;
            });
        },
        getMessageCenterList: function () {
       	 	$.getJSON("sys/messagecenter/mainlist?_" + $.now(), function (r) {
                vm.messageceneterList = r.messageList;
                vm.messagecenetercount=r.messagecount;
            });
        },
        getUser: function () {
            $.getJSON("sys/user/info?_" + $.now(), function (r) {
                vm.user = r.user;
            });
        },
        updatePassword: function () {
        	console.log(1222);
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "修改密码1",
                area: ['550px', '270px'],
                shadeClose: false,
                content: jQuery("#passwordLayer"),
                btn: ['修改', '取消'],
                btn1: function (index) {
                    var data = "password=" + vm.password + "&newPassword=" + vm.newPassword;
                    $.ajax({
                        type: "POST",
                        url: "sys/user/password",
                        data: data,
                        dataType: "json",
                        success: function (result) {
                            if (result.code == 0) {
                                layer.close(index);
                                layer.alert('修改成功', function (index) {
                                    location.reload();
                                });
                            } else {
                                layer.alert(result.msg);
                            }
                        }
                    });
                }
            });
        },
        updateUserInfo: function () {
            layer.open({
                type: 2,
                offset: '20px',
                skin: 'layui-layer-molv',
                title: "修改个人信息",
                area: ['620px', '550px'],
                shade: 0.3,
                shadeClose: false,
                content: "modules/sys/updateUserInfo.html?userGuid="+vm.user.userguid,
                btn: [ '取消'],
                btn1: function (index) {
                }
            });
        },
        donate: function () {
            layer.open({
                type: 2,
                title: false,
                area: ['806px', '467px'],
                closeBtn: 1,
                shadeClose: false,
                content: ['http://cdn.renren.io/donate.jpg', 'no']
            });
        }
        
    },
    showdbview: function (url) {
        layer.open({
            type: 2,
            offset: '20px',
            skin: 'layui-layer-molv',
            title: "查看代办",
            area: ['920px', '450px'],
            shade: 0.3,
            shadeClose: false,
            content: url

        });
    },
    created: function () {
        this.getMenuList();
        this.getUser();
        this.getMessageList();
        this.getMessageCenterList();
    },
    updated: function () {
        //路由
        var router = new Router();
        routerList(router, vm.menuList);
        router.start();
    }
});


function routerList(router, menuList) {
    for (var key in menuList) {
        var menu = menuList[key];
        if (menu.moduletype == 0) {
            routerList(router, menu.list);
        } else if (menu.moduletype == 1) {
            router.add('#' + menu.moduleurl, function () {
                var url = window.location.hash;

                //替换iframe的url
                vm.main = url.replace('#', '');

                //导航菜单展开
                $(".treeview-menu li").removeClass("active");
                $("a[href='" + url + "']").parents("li").addClass("active");

                vm.navTitle = $("a[href='" + url + "']").text();
            });
        }
    }
}
